package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Flow;
import teacher.bean.Paper;
import teacher.bean.Project;
import teacher.bean.Teacher;
import teacher.service.flow;
import teacher.service.project;
import tools.StringDate;

import com.opensymphony.xwork2.ActionSupport;

public class Upproject extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private Project project;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	private String dates1;
	private String dates2;
	String suc;
	
	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getDates1() {
		return dates1;
	}

	public void setDates1(String dates1) {
		this.dates1 = dates1;
	}

	public String getDates2() {
		return dates2;
	}

	public void setDates2(String dates2) {
		this.dates2 = dates2;
	}

	public String execute() throws Exception {
		
		if(project.getArriveFunds()==null){
			project.setArriveFunds(0);
		}
		if(project.getLowDepartment()==null){
			project.setLowDepartment("0");
		}
		if(project.getLevel()==null){
			project.setLevel(0);
		}

		if(project==null){
			return ERROR;
			
		}
		project.setState(0);
		HttpServletRequest request=ServletActionContext.getRequest();
		Teacher tea=(Teacher)request.getSession().getAttribute("teacher");
		int teaId=tea.getTeaId();
		project.setTeaId(teaId);
		
		if(dates1!=null&&dates1.length()>0)
		{
			project.setBeginDate(new StringDate().StringToDate(dates1));
			project.setFinishDate(new StringDate().StringToDate(dates2));
		}
		else
		{
			project.setBeginDate(new Date());
			project.setFinishDate(new Date());
		}
		
		System.out.println("1");
		if(fileCom!=null)
		{
			
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		project.setUpload(fileComFileName);
		}
		if(new project().insertProject(project)) {
			this.setSuc("success");
			
//			流程管理部分
			Project p=new Project();
			p=new flow().selectProjectByName(this.getProject().getName().toString());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Flow f=new Flow();
			f.setFruitId(p.getProId());
			f.setFruitKind(4);
			f.setDirDo(2);
			f.setDirTime(df.format(new Date()));
			f.setSubDo(2);
			f.setSubTime(df.format(new Date()));
			f.setTeaDo(0);
			f.setTeaTime(df.format(new Date()));
//			流程管理部分尾
			if(new flow().insertFlow(f)){
				return SUCCESS;		
			}else{
				return SUCCESS;		
			}
		} else {
			return ERROR;
		}
	}

}